<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung.
    *
    */

	class model_counter extends my_model
	{

        protected $_table_useronline  = 'tbl_useronline';
        protected $_table_usercounter = 'tbl_counter';

		public function __construct()
        {
            parent::__construct();

        }

        public function getUserOnline()
        {
            $tg = time();

            $insertUser = array(
                'session_id'    => $_COOKIE['PHPSESSID'],
                'tgtmp'         => time(),
                'ip'            => $_SERVER['REMOTE_ADDR'],
                'local'         => $_SERVER['PHP_SELF'],
                'user_agent'    => $_SERVER['HTTP_USER_AGENT']
            );

            $this->where(array('session_id' => $insertUser['session_id']));
            $this->get($this->_table_useronline);

            if($this->num_rows() == 0)
            {
                $this->insert($this->_table_useronline, $insertUser);
            }

            $tgout = 900;/*DEFAULT = 15 minutes*/

            $tgnew = $tg - $tgout;

            $this->where('tgtmp < '.$tgnew);
            $this->delete($this->_table_useronline);

            $num_users = $this->get($this->_table_useronline);

            /*CHEAT*/
            if($num_users == 0)
            {
                $num_users = 1;
            }

            return $num_users;

        }

        public function getCounter()
        {
            $today = getdate();

            $data = array(
                'session_id' => $_COOKIE['PHPSESSID'],
                'time'       => date('Y-m-d'),
                'day'        => $today['mday'],
                'month'      => $today['mon'],
                'year'       => $today['year']
            );

            $this->where($this->_table_usercounter, array('session_id' => $data['session_id']));

            if($this->num_rows() == 0)
            {
                $this->insert($this->_table_usercounter, $data);
            }

            /***Ngày hi?n t?i***/
            $condition_day = array(
                'day'   => $today['mday'],
                'month' => $today['mon'],
                'year'  => $today['year']
            );

            $this->where($condition_day);
            $this->get($this->_table_usercounter);

            $day_counter = $this->num_rows();

            /*HACK*/
            $day_counter = $day_counter + 4;

            /***Tháng hi?n t?i***/
            $condition_month = array(
                'month' => $today['mon'],
                'year'  => $today['year']
            );

            $this->where($condition_month);
            $this->get($this->_table_usercounter);

            $month_counter = $this->num_rows();

            /*HACK*/
            $month_counter = $month_counter + 30;

            /***T?ng truy c?p***/
            $this->get($this->_table_usercounter);
            $total = $this->num_rows();

            /*HACK*/
            $total = $total + 122;

            $result = array(
                'day_counter'   => $day_counter,
                'month_counter' => $month_counter,
                'total_counter' => $total
            );

            return $result;
        }


	}


    /*End of .php*/
    /*Location: my_framework/.php */